using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using MimeCloud.AssetManagement.Business.Interface;
using MimeCloud.AssetManagement.Editor.AssetCreation.Repository;
using MimeCloud.AssetManagement.ProviderServices.ContentTypes;
using MimeCloud.AssetManagement.Business;
using MimeCloud.AssetManagement.Editor;
using System.Collections;
using MimeCloud.WPF.Imaging;

namespace MimeCloud.AssetManagement.Editor.AssetCreation
{
    public class Image : IContentTypeForImport
    {
        public static IAssetContent CreateContent (string fileLocation)
        {
            var result = LocallyStoredAssetContent.Create(LocallyStoredAssetContent.LocalStoreType.FileSystem);
            (result.AssetContentRepositoryList[0] as LocalFileStoreSingleAssetRepository).URLPrefix = fileLocation;
            result.AssetPath = Path.GetFileName(fileLocation);
            //attempt to read source (needed to verify content and get type details)
            BitmapImage source;
            try
            {
                source = Reading.ReadImageFromData(fileLocation);
            }
            catch (Exception e)
            {
                throw new ArgumentException("Unable to read image",e);
            }

            PopulateContent(result,source);
           
            return result;
        }
        public static IAssetContent CreateContent(byte[] content, string fileName)
        {
            var result = LocallyStoredAssetContent.Create(LocallyStoredAssetContent.LocalStoreType.Memory);
          
            result.AssetPath = Path.GetFileName(fileName);
            BitmapImage source;
            try
            {
                source = Reading.ReadImageFromData(content);
            }
            catch (Exception e)
            {
                throw new ArgumentException("Unable to read image",e);
            }
            (result.AssetContentRepositoryList[0] as MemorySingleAssetRepository).AssetContent = Writing.GetEncodedImageData(source,".jpg");
            PopulateContent(result,source);
            
            return result;
        }
        private static void PopulateContent (IAssetContent result, BitmapImage source)
        {
            //set content type
            IAssetContentType ct = GetTypeForImage(source);
            if (ct == null)
                throw new ArgumentException("Image was not of a known size");
            result.AssetContentType = ct;
            result.AssetContentTypeID = ct.ID;

            //set mime type
            result.MimeType = GetMimeTypeForJpegImage();
            result.MimeTypeID = result.MimeType.ID;
        }

        public static IAssetContentType GetTypeForImage(BitmapImage data)
        {
            List<IAssetContentType> imageTypes =
                AppCommon.Current.AllAssetContentTypes.Where(t => t.ContentType == TypeOfContent.Image).ToList();
            var matchingTypes =
                imageTypes.Where(
                    t => data.Width == Convert.ToDouble(t.Width) && data.Height == Convert.ToDouble(t.Height));
            if (matchingTypes.Count() == 0)
                return null;

            return matchingTypes.First();
        }

        public static IMimeType GetMimeTypeForJpegImage()
        {
            List<IMimeType> imageMimeTypes =
                AppCommon.Current.AllMimeTypes.Where(
                    m => m._MimeType.ToLower().StartsWith("image", StringComparison.InvariantCultureIgnoreCase)).ToList();
            if (imageMimeTypes.Count == 0)
                throw new Exception("Database does not support images");
            //todo: support more types than just jpeg
            return imageMimeTypes[0];
        }

    
        #region IContentTypeForImport Members

        public string FileFilter
        {
            get
            {
                return "*.jpg,*.png,*.gif";
            }
            
        }

        #endregion
    }
}
